from django.db import models

from user.models import Role, User


# Create your models here.

class Language(models.Model):
    name = models.CharField(max_length=150)
    description = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)
    user_id = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "tb_language"
        verbose_name = "语言"
        verbose_name_plural = "语言"


class Module(models.Model):
    name = models.CharField(max_length=150)
    language_id = models.ForeignKey(Language, on_delete=models.CASCADE)
    description = models.TextField()
    order_num = models.IntegerField(blank=True, null=True, default=0, db_comment='排序')
    created_time = models.DateTimeField(auto_now_add=True)
    user_id = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "tb_module"
        verbose_name = "模型"
        verbose_name_plural = "模型"




class FunctionManagement(models.Model):
    name = models.CharField(max_length=150)
    fun_code = models.CharField(blank=True, null=True, max_length=150)
    url = models.CharField(max_length=255, blank=True, null=True, db_comment="功能访问路径")
    module = models.ForeignKey(Module, on_delete=models.SET_NULL, null=True, related_name="functions",
                               db_comment="所属模块（可选）")
    description = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True, db_comment='是否启用')

    class Meta:
        db_table = "tb_function_management"
        verbose_name = "功能管理"
        verbose_name_plural = "功能管理"


class PermissionAssignment(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name="role_permissions", db_comment="关联角色")
    basic_module = models.CharField(blank=True, null=True, max_length=150, db_comment="基础模块", default='000')
    business_module = models.CharField(blank=True, null=True, max_length=150, db_comment="业务模块", default='000')
    created_time = models.DateTimeField(auto_now_add=True, db_comment="分配时间")

    class Meta:
        db_table = "tb_permission_assignment"
        verbose_name = "权限分配"
        verbose_name_plural = "权限分配"